package com.ruoyi.project.system.controller;

import java.lang.reflect.Array;
import java.util.*;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.TChoice;
import com.ruoyi.project.system.service.ITChoiceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TMultisel;
import com.ruoyi.project.system.service.ITMultiselService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.naming.Name;

/**
 * 多选题库 Controller
 * 
 * @author ruoyi
 * @date 2020-05-09
 */
@RestController
@RequestMapping("/system/multisel")
public class TMultiselController extends BaseController
{
    @Autowired
    private ITMultiselService tMultiselService;
    @Autowired
    private ITChoiceService tChoiceService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询多选题库 列表
     */
    @PreAuthorize("@ss.hasPermi('system:multisel:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMultisel tMultisel)
    {
        startPage();
        List<TMultisel> list = tMultiselService.selectTMultiselList(tMultisel);
        return getDataTable(list);
    }

    /**
     * 导出多选题库 列表
     */
    @PreAuthorize("@ss.hasPermi('system:multisel:export')")
    @Log(title = "多选题库 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMultisel tMultisel)
    {
        List<TMultisel> list = tMultiselService.selectTMultiselList(tMultisel);
        ExcelUtil<TMultisel> util = new ExcelUtil<TMultisel>(TMultisel.class);
        return util.exportExcel(list, "multisel");
    }

    /**
     * 获取多选题库 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:multisel:query')")
    @GetMapping(value = "/{multiselId}")
    public AjaxResult getInfo(@PathVariable("multiselId") Integer multiselId)
    {
        return AjaxResult.success(tMultiselService.selectTMultiselById(multiselId));
    }

    /**
     * 新增多选题库 
     */
    @PreAuthorize("@ss.hasPermi('system:multisel:add')")
    @Log(title = "多选题库 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMultisel tMultisel)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        tMultisel.setUserId(user.getUserId().intValue());
        tMultiselService.addTMultiselBMiddle(tMultisel.getCourseId(),tMultisel.getMultiselId());
        return toAjax(tMultiselService.insertTMultisel(tMultisel));
}

    /**
     * 修改多选题库 
     */
    @PreAuthorize("@ss.hasPermi('system:multisel:edit')")
    @Log(title = "多选题库 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMultisel tMultisel)
    {
        tMultiselService.updateTMultiselBMiddle(tMultisel.getCourseId(),tMultisel.getMultiselId(),
                tMultisel.getCourseName());
        return toAjax(tMultiselService.updateTMultisel(tMultisel));
    }

    /**
     * 删除多选题库 
     */
    @PreAuthorize("@ss.hasPermi('system:multisel:remove')")
    @Log(title = "多选题库 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{multiselIds}")
    public AjaxResult remove(@PathVariable Integer [] multiselIds)
    {
        return toAjax( tMultiselService.deleteTMultiselByIds(multiselIds));
    }

    /**
     * 查询多选题相应的选项
     */
    @PostMapping("/choice")
    @ResponseBody
    public AjaxResult getMultiselChoiceList(Integer multiselId )
    {
        TChoice tChoice = new TChoice();
        tChoice.setQuestionId(multiselId);
        List<TChoice> tChoices = tChoiceService.selectTChoiceList(tChoice);
        if(tChoices!=null&&tChoices.size()>0){
            return AjaxResult.success(tChoices);
        }else {
            return AjaxResult.error("操作失败");
        }
    }

}
